package com.ase.寻找数组的中心索引;

/**
 * @author gyhstart
 * @create 2021/2/19 - 18:33
 **/
public class Solution {
    public static void main(String[] args) {
        int[] nums = {1, 7, 3, 6, 5, 6};
        int i = pivotIndex(nums);
        System.out.println(i);
    }

    public static int pivotIndex(int[] nums) {
        int sum = 0;
        int leftSum = 0;

        //得到nums的总和
        for (int i : nums) {
            sum += i;
        }

        for (int x = 0; x < nums.length; x++) {
            if (2*leftSum + nums[x] == sum){
                return x;
            }
            leftSum += nums[x];
        }
        return -1;
    }
}
